import { defineStore } from 'pinia'
import { getGouwuData, removeDanData ,updateData ,updateFlag,updateFlagAll,removeAll} from '@/api/gouwu'
import { addDingdan } from '@/api/dingdan'
import _ from 'lodash'


type state = {
    userGouwu: gouwu[] ,
    checkbox: Array<gouwu>,
    alls: boolean,
}

export const useGouwu = defineStore(
    'gouwu',
    {
        state: (): state => {
            return {
                // 订单信息
                userGouwu: [],
                // 选中
                checkbox: [],
                // 全选
                alls: true,
            }

        },
        getters: {
            // 计算选中价格
            num(state) {
                return state.checkbox.reduce((p,i)=>p+i.num*i.originprice*i.discount/10,0)*100
            }
        },
        actions: {
            // 退出登录清除数据
            removeUse(){
                this.checkbox = [];
                this.userGouwu = [];
            },
            async fecthGetData() {
                // 初始获取将数据为空
                this.checkbox = [];
                if (localStorage.getItem('token')) {
                    let token = localStorage.getItem('token') as string;
                    let userid = localStorage.getItem('userId') as string;
                    let res = await getGouwuData(token, userid);
                    // 获取用户下的购物数据
                    this.userGouwu = res.data;
                    this.userGouwu?.forEach(item =>{
                        // 遍历数据 如果数据flag为true 证明被选中 添加到v-model的checkbox中
                        if(item.flag){
                            this.checkbox.push(item)
                        }
                    })
                    // 第一次传输数据就进行一次全选是否勾选
                    this.changeData();
                }
            },
            // 改变是否全选
            changeAlls() {
                if (this.alls) {
                    this.checkbox = [];
                    this.userGouwu?.forEach(item => this.checkbox.push(item))
                } else {
                    this.checkbox = [];
                }
            },
            // 发送最新的全选状态
            getAlls(){
                updateFlagAll(localStorage.getItem('userId') as string,this.alls)
            },
            // 点击订单
            changeData() {
                if (this.checkbox.length == this.userGouwu?.length) {
                    this.alls = true
                } else {
                    this.alls = false;
                }
            },
            // 发送单条数据的 选中状态
            getFlag(obj:gouwu){
                updateFlag(obj.cartid,!obj.flag)
            },
            // 点击删除订单
            removeData(obj: gouwu) {
                let index = this.userGouwu?.findIndex(item => item == obj);
                let index2 = this.checkbox.findIndex(item => item == obj);
                removeDanData(obj.cartid);
                this.userGouwu?.splice(index as number, 1)
                if (index2 != -1) {
                    this.checkbox.splice(index2, 1)
                }
                this.changeData()
            },
            // 点击订单的减按钮
            sub(obj: gouwu) {
                obj.num--;
            },
            addData(obj: gouwu) {
                obj.num++;
            },
            // 发送订单的选中状态
            updateData(obj:gouwu){
                updateData(obj.cartid,obj.num)
            },
            async removeDataALLs(){
                this.checkbox = [];
                this.userGouwu = [];
                let res =await removeAll(localStorage.getItem('userId') as string);
                console.log(res);
            },
            // 提交订单时删除数据函数
            // 提交订单
            async submits(){
                // 遍历选中数据删除数据 添加到订单
                this.checkbox.forEach(item=>{
                    let findIndex = this.userGouwu.findIndex(i=>i == item);
                    this.userGouwu.splice(findIndex,1);
                    removeDanData(item.cartid)
                })
                let res = await addDingdan(localStorage.getItem("userId") as string);
                console.log(res);
                
            }
        }
    }
)